<template>
  <div class="bottom-bar">
    <div class="check-content">
      <check-button 
        :is-checked="isSelectAll" 
        class="check-button"
        @click.native="checkClick"/>
      <span>全选</span>
    </div>

    <div class="price">
      合计：{{tottalPrice}}
    </div>

    <div class="calculate" @click="calcClick">
      去计算：({{checkLength}})
    </div>
  </div>
</template>

<script>
import checkButton from '@/components/content/checkButton/CheckButton.vue';

export default {
  name: "CartBottomBar",
  components: {
    checkButton 
  },
  computed: {
    tottalPrice() {
      return '￥' + this.$store.state.cartList.filter(item => {
        return item.checked
      }).reduce((preValue, item) => {
        return preValue + item.price * item.count
      }, 0)
    },
    checkLength() {
      return this.$store.state.cartList.filter(item => item.checked).length
    },
    isSelectAll() {
      if (this.$store.state.cartList.length === 0) return false
      
      return !this.$store.state.cartList.find(item => !item.checked)
    }
  },
  methods: {
    checkClick() {
      if (this.isSelectAll) {  // 全部选中
        this.$store.state.cartList.forEach(item => item.checked = false)
      } else {  //有部分或全部未选中
        this.$store.state.cartList.forEach(item => item.checked = true)
      }
    },

    calcClick() {
      if (!this.isSelectAll) {
        this.$toast.show('请选择购买的商品', 2000)
      }
    }
  }
}
</script>

<style scoped>
  .bottom-bar {
    position: relative;
    display: flex;

    height: 40px;
    line-height: 40px;

    background-color: #eee;

    font-size: 14px;
  }

  .check-content {
    display: flex;
    line-height: 40px;
    align-items: center;
    margin-left: 10px;
    width: 60px;
  }

  .check-button {
    width: 20px;
    height: 20px;
    /* margin-left: 5px; */
    line-height: 20px;
    /* margin: 0; */
  }

  .price {
    margin-left: 20px;
    flex: 1;
  }

  .calculate {
    width: 100px;
    background: red;
    text-align: center;
  }
</style>